using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using LeisureCenter.Api;
using LeisureCenter.BrowserItems;
using LeisureCenter.OptionsForms;

namespace LeisureCenter.MenuItems
{
    public abstract class BaseFileSystemMenuItem
    {
        public FileSystemBrowserFolderItem lastFolder;
        protected FileSystemBrowserFolderItem rootFolder;

        public void Execute()
        {
            if (lastFolder == null)
                ExecuteAgain();
            else
                Program.LeisureCenterInstance.BrowserManager.Browse(lastFolder);
        }

        public virtual void ExecuteAgain()
        {
            if (rootFolder == null)
                rootFolder = CreateRootFolder();
            if (rootFolder != null)
                Program.LeisureCenterInstance.BrowserManager.Browse(rootFolder);
        }

        public virtual void OptionsExecute()
        {
        }

        public abstract FileSystemBrowserFolderItem CreateRootFolder();
    }

    public class FileSystemMenuItem : BaseFileSystemMenuItem, IMenuItem
    {
        private string path;
        private string iconName;
        private string volumeLabel;

        public FileSystemMenuItem(string volumeLabel, string path, string iconName)
        {
            this.path = path;
            this.iconName = iconName;
            this.volumeLabel = volumeLabel;
        }

        public override FileSystemBrowserFolderItem CreateRootFolder()
        {
            return new FileSystemBrowserFolderItem(this, null, new DirectoryInfo(path), null);
        }

        #region IMenuItem Membres

        public virtual string Text
        {
            get
            {
                if (!string.IsNullOrEmpty(volumeLabel))
                    return path + Environment.NewLine + volumeLabel;
                else
                    return path;
            }
        }

        public string IconName { get { return iconName; } }

        public override void OptionsExecute()
        {
            FileSystemFolderOptionsDialog dialog = new FileSystemFolderOptionsDialog(this.path);
            using (dialog)
            {
                dialog.Execute();
            }            
        }

        #endregion
    }

    public class FileSystemMainMenuItem : IMenuItem
    {
        List<IMenuItem> items;

        #region IMenuItem Membres

        public string Text { get { return @"FileSystem"; } }

        public string IconName { get { return "drive"; } }

        public void Execute()
        {
            if (items != null)
                Program.LeisureCenterInstance.MenuManager.FillSubmenu(items);
            else
                ExecuteAgain();
        }

        public void ExecuteAgain()
        {
            items = new List<IMenuItem>();

            // drives
            foreach (DriveInfo driveInfo in DriveInfo.GetDrives())
            {
                if (!driveInfo.Name.StartsWith("A", StringComparison.OrdinalIgnoreCase)
                    && !driveInfo.Name.StartsWith("B", StringComparison.OrdinalIgnoreCase))
                {
                    if (driveInfo.DriveType == DriveType.Fixed)
                        items.Add(new FileSystemMenuItem(driveInfo.VolumeLabel, driveInfo.Name, "drive"));
                    else
                        if (driveInfo.DriveType == DriveType.Network)
                            items.Add(new FileSystemMenuItem("", driveInfo.Name, "drive"));
                }
            }

            Program.LeisureCenterInstance.MenuManager.FillSubmenu(items);
        }

        public void OptionsExecute()
        {
        }
        #endregion
    }

    public class FileSystemMainMenuItemsProvider : IMainMenuItemsProvider
    {
        IMenuItem[] items = new IMenuItem[] { new FileSystemMainMenuItem() };

        #region IMainMenuItemsProvider Membres

        public IEnumerable<IMenuItem> Items { get { return items; } }

        #endregion
    }
}
